package tt.dz.egobus.servicemanager.vo;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import tt.dz.egobus.bean.BaseBean;
import tt.dz.egobus.bean.CodeStatus;
import tt.dz.egobus.bean.CommonStatus;
import tt.dz.egobus.servicemanager.bean.ServiceManagerCodeStatus;
import tt.dz.egobus.servicemanager.core.exception.BusinessException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static tt.dz.egobus.servicemanager.bean.ServiceManagerCodeStatus.PARAM_ILLEGAL_ERROR;

/**
 * @author Anson
 * @date 17-3-14
 * @name
 * @since 1.0.1
 */
public class JsonBack<T> implements BaseBean {

    private static final long serialVersionUID = -289244973858357705L;
    private boolean success;

    private int code;

    private String errorMsg;

    private T t;


    public JsonBack(CodeStatus codeStatus) {
        this.code = codeStatus.getCode();
        this.errorMsg = codeStatus.getMsg();
        this.dealSuccess();
    }

    public JsonBack(CodeStatus codeStatus, T t) {
        this.code = codeStatus.getCode();
        this.errorMsg = codeStatus.getMsg();
        this.t = t;
        this.dealSuccess();
    }

    public JsonBack(int code, String errorMsg, T t) {
        this.code = code;
        this.errorMsg = errorMsg;
        this.t = t;
        this.dealSuccess();
    }

    public JsonBack() {
        this.dealSuccess();
    }

    public static final <T> JsonBack<T> instance(CodeStatus codeStatus, T t) {
        return new JsonBack(codeStatus, t);
    }

    public static final <T> JsonBack<T> instance(int code, String errorMsg, T t) {
        return new JsonBack(code, errorMsg, t);
    }

    public static final <T> JsonBack<T> success(T t) {
        return new JsonBack(CommonStatus.SUCCESS, t);
    }

    public static final JsonBack error(ServiceManagerCodeStatus status) {
        return new JsonBack(status.getCodeStatus());
    }

    /**
     * @param e 异常错误
     * @author Anson
     * @date 17-3-15
     * @name 返回错误异常
     * @since 1.0.1
     */
    public static final JsonBack error(BusinessException e) {
        String errorMeg = e.getMsg() + ":" + e.getMessage();
        int code = e.getCode();
        return new JsonBack(code, errorMeg, null);
    }

    /**
     * 获取绑定数据错误
     *
     * @param result
     * @return
     */
    public static final Map<String, String> getBackErrors(BindingResult result) {
        Map<String, String> map = new HashMap<String, String>();
        List<FieldError> list = result.getFieldErrors();
        for (FieldError error : list) {
            map.put(error.getField(), error.getDefaultMessage());
        }

        return map;
    }

    /**
     * @param result 错误绑定异常
     * @throws BusinessException
     * @author Anson
     * @date 17-3-15
     * @name 抛出错误异常
     * @since 1.0.1
     */
    public static final void throwErrorException(BindingResult result) throws BusinessException {
        Map<String, String> errorMap = getBackErrors(result);
        if (null != errorMap && !errorMap.isEmpty()) {
            StringBuilder errorBuilder = new StringBuilder();
            Map.Entry<String, String> entry = null;
            for (Iterator<Map.Entry<String, String>> iterator = errorMap.entrySet().iterator(); iterator.hasNext(); ) {
                entry = iterator.next();
                errorBuilder.append("错误信息：[" + entry.getValue() + "]");
            }
            String error = errorBuilder.toString();
            errorBuilder.setLength(0);
            throw new BusinessException(PARAM_ILLEGAL_ERROR.getCodeStatus(), error);
        }

    }

    private void dealSuccess() {
        this.success = this.code == CommonStatus.SUCCESS.getCode();
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }

    @Override
    public String toString() {
        return "JsonBack{" +
                "success=" + success +
                ", code=" + code +
                ", errorMsg='" + errorMsg + '\'' +
                ", t=" + t +
                '}';
    }
}
